#define MAX_AMPLITUDE 32767
#define SYNC_WORD 0xBFFC

typedef struct tagDVD_TIMECODE {
   unsigned int Hours1			:4; 
   unsigned int Hours10			:4; 
   unsigned int Minutes1		:4; 
   unsigned int Minutes10		:4; 
   unsigned int Seconds1		:4; 
   unsigned int Seconds10		:4; 
   unsigned int Frames1			:4; 
   unsigned int Frames10		:2; 
   unsigned int FrameRateCode	:2; 
} DVD_TIMECODE;

class CLTCGen
{
	unsigned char smpte_block[10];
	unsigned int m_TimeCode;
	DVD_TIMECODE *m_DVDTimeCode;
	unsigned int m_TimeCodeStart;
	DVD_TIMECODE *m_DVDTimeCodeStart;
	bool bSquare;

	int m_iSampleRate;
	int m_iChannelCount;
	int m_iFPS;
	bool bDropFrame;
	double m_dAmplify;
	int m_iFrameDur;
	int m_iBitDur;
	int m_iRiseDur;
	int m_iExtraSamples;
	int m_iAmplitude;
	int m_iMaxAmplitude;

	short *mWav0Pos;
	short *mWav0Neg;
	short *mWav1Pos;
	short *mWav1Neg;

	short *mWav0PosEx;
	short *mWav0NegEx;
	short *mWav1PosEx;
	short *mWav1NegEx;

	void LTC_EncodeTime(unsigned char *block, DVD_TIMECODE *TimePos, bool dropframe);
	void LTC_UserBits(unsigned char *block, unsigned int userbits);
	void LTC_Parity(unsigned char *block, int frame_rate);
	bool InitWaveForms();

public:
	CLTCGen();
	~CLTCGen(void);
	bool Init(DVD_TIMECODE *DVDTimeCodeStart, int SampleRate,
		int ChannelCount, int FPS, bool DropFrame, double Amplify, bool Square);
	unsigned int Generate(short *sample, unsigned int sampleStart, unsigned int numSamples);
	unsigned int Generate2(short *sample, unsigned int sampleStart, unsigned int numSamples);
	unsigned int Generate3(short *sample, unsigned int sampleStart, unsigned int numSamples);
	unsigned int LTC_SampleNum_to_BCD(unsigned int SampleNum, int SampleRate, DVD_TIMECODE *TimePos, DVD_TIMECODE *TimeOffset, bool DropFrame);
};